<?php
namespace App\Http\Controllers\Admin;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
class TeacherController extends Controller{
	public function __construct()
    {
        $this->middleware('admin');
    }
    public function teacher()
    {
        return view('admin.teacher.teacher');
    }
    public function teacherdata(Request $request){
        $key=$request->input('key');
        $state=$request->input('state');
        $recommend=$request->input('recommend');
        if (!empty($request->input('start_time'))) {
            $start_time=strtotime($request->input('start_time'));
        }else{
            $start_time=0;
        }
        if (!empty($request->input('end_time'))) {
            $end_time=strtotime($request->input('end_time'));
        }else{
            $end_time=time();
        }
        $map = [
            ['time', '>', $start_time],
            ['time', '<', $end_time]
        ];
        if($state==0){
            array_push($map,['state','<',3]);
        }else{
            array_push($map,['state',$state]);
        }
        if($recommend==0){
            array_push($map,['recommend','<',3]);
        }else{
            array_push($map,['recommend',$recommend]);
        }
        if(!empty($key)){
            $count = DB::table('teacher')
            ->whereRaw("concat(`teacher`,`account`) like '%".$key."%'")
            ->where($map)
            ->count();
        }else{
            $count = DB::table('teacher')
            ->where($map)
            ->count();
        }
        $pageNum=$request->input('page');
        $limit=$request->input('limit');
        $page=$pageNum-1;
        if ($page != 0) {
            $page = $limit * $page;
        }
        if(!empty($key)){
            $users = DB::table('teacher')
            ->whereRaw("concat(`teacher`,`account`) like '%".$key."%'")
            ->where($map)
            ->orderBy('id','desc')
            ->offset($page)
            ->take($limit)
            ->get();
        }else{
            $users = DB::table('teacher')
            ->where($map)
            ->orderBy('id','desc')
            ->offset($page)
            ->take($limit)
            ->get();
        }
        foreach ($users as $k=>$item){
            $users[$k]->time =date('Y-m-d H:i:s',$users[$k]->time);
            $users[$k]->uptime =date('Y-m-d H:i:s',$users[$k]->uptime);
        }
        $data = [
            'code' => 0,
            'msg'   => '正在请求中...',
            'count' => $count,
            'data'  => $users
        ];
        return response()->json($data);
    }
    public function addteacher(Request $request)
    {
        if ($request->isMethod('post')) {
            $res=DB::table('teacher')->where('account',$request->account)->first();
            if($res){
                $data = [
                    'code' => -1,
                    'msg'   => '存在此账号',
                ];
                return response()->json($data);
            }
            DB::table('teacher')->insert(['teacher' => $request->teacher,'avatar' => $request->avatar, 'account' => $request->account, 'state' =>1, 'recommend' =>2, 'password' =>Hash::make($request->password), 'workage' =>$request->workage,'intro' =>$request->intro, 'time' =>time() ,'uptime' =>time()]);
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
        return view('admin.teacher.addteacher');
    }
    public function editteacher(Request $request)
    {
        $res=DB::table('teacher')->where('id',$request->id)->first();
        $id=$request->id;
        $teacher=$res->teacher;
        $account=$res->account;
        $workage=$res->workage;
        $avatar=$res->avatar;
        $intro=$res->intro;
        return view('admin.teacher.editteacher', compact('id','teacher','avatar','account','workage','intro'));
    }
    public function editteacherup(Request $request)
    {
        if ($request->isMethod('post')) {
            if($request->password){
                $password=Hash::make($request->password);
                DB::table('teacher')->where('id',$request->id)->update(['teacher' => $request->teacher, 'avatar' => $request->avatar, 'account' =>$request->account, 'workage' =>$request->workage,'intro' =>$request->intro,'password' =>$password,  'uptime' =>time()]);
            }else{
                DB::table('teacher')->where('id',$request->id)->update(['teacher' => $request->teacher, 'avatar' => $request->avatar, 'account' =>$request->account, 'workage' =>$request->workage,'intro' =>$request->intro,  'uptime' =>time()]);
            }
            $data = [
                'code' => 0,
                'msg'   => '完成',
            ];
            return response()->json($data);
        }
    }
    public function delteacher(Request $request)
    {
            $res=DB::table('teacher')->where('id',$request->id)->update(['state'=>3]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function state_off(Request $request)
    {
            $res=DB::table('teacher')->where('id',$request->id)->update(['state'=>2]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function state_on(Request $request)
    {
            $res=DB::table('teacher')->where('id',$request->id)->update(['state'=>1]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function recommend_off(Request $request)
    {
            $res=DB::table('teacher')->where('id',$request->id)->update(['recommend'=>2]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
    public function recommend_on(Request $request)
    {
            $res=DB::table('teacher')->where('id',$request->id)->update(['recommend'=>1]);
            if($res){
                $data = [
                    'code' => 200,
                    'msg'   => '完成',
                ];
                return response()->json($data);
            }else{
                $data = [
                    'code' => 500,
                    'msg'   => '出错',
                ];
                return response()->json($data);
            }
    }
}